package com.it.myd.service.main.module.room;

import com.it.myd.bean.box.game.Room;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoomServiceImpl implements RoomService{

    private Map<String, Room> rooms=new HashMap<>();
    private Map<Integer,String>userIdToRoomID=new HashMap<>();

    public void add(Room room,List<Integer> useridlist) {
        rooms.put(room.getRoom_id(), room);
        for (Integer ids : useridlist) {
            userIdToRoomID.put(ids, room.getRoom_id());
        }
    }

    public  void remove(String roomId,List<Integer> useridlist){
        rooms.remove(roomId);
        for (Integer ids : useridlist) {
            userIdToRoomID.remove(ids);
        }
    }

    public Room getRoomByRoomId(String roomId){
        return rooms.get(roomId);
    }


    public Room getRoomByUserId(Integer userId){
        String roomId=userIdToRoomID.get(userId);
        if(roomId==null){
            return  null;
        }
        return rooms.get(roomId);
    }

}
